package cn.lyvust;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class LuckyStudy extends JFrame {
    public int index = 0;
    public int lines = 0;
    //用来存储所有学生的姓名
    public List<String> names = new ArrayList<String>();


    public static void main(String[] args) {
        new LuckyStudy();
    }

    public LuckyStudy() {
        this.setTitle("20级计算机五班抽奖程序");
        this.setSize(350, 200);
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        //设置窗体为自定义布局
        this.setLayout(null);
        this.getNames();
        this.init();
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void init() {
        JButton jbStart = new JButton("开始");
        JButton jbEnd = new JButton("结束");
        //设置按钮的位置和大小
        jbStart.setBounds(50, 107, 93, 37);
        jbEnd.setBounds(180, 107, 93, 37);
        //将按钮设置到窗体上
        this.add(jbStart);
        this.add(jbEnd);

        JLabel jLStart = new JLabel("开始");
        jLStart.setFont(new Font(Font.DIALOG, Font.BOLD, 30));
        jLStart.setBounds(90, 45, 120, 30);
        this.add(jLStart);

        //定时器
        Timer timer = new Timer(50, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int random = 0;
                Random rand = new Random();
                random = rand.nextInt(lines);
                jLStart.setText(names.get(index));
                index += random;
                if (index >= names.size() && lines != 0) {
                    index = index % lines;
                }
            }
        });
        //按钮的响应函数，即:每当点击按钮的时候触发的函数/动作
        jbStart.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("你点击了一下开始按钮");
                timer.start();
            }
        });
        jbEnd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                timer.stop();
            }
        });
    }

    //读取names.txt里面的所有的姓名
    public void getNames() {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader("names.txt"));
            String name = br.readLine();
            while (name != null) {
                names.add(name);
                name = br.readLine();
                lines ++;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //文件读取结束后，要关闭资源
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
